package jp.tuyano.sample.client.search;

import java.util.List;

import jp.tuyano.sample.client.form.ReferenceForm;
import jp.tuyano.sample.shared.DataConvert;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Search implements EntryPoint {

    private final SearchServiceAsync searchService =
        GWT.create(SearchService.class);
    
    private Label label;
    private TextArea scArea;
    
    public void onModuleLoad() {

        label = new Label("検索条件をJDOQLで入力してください。");
        RootPanel.get("search_label").add(label);
        
        VerticalPanel scPanel = new VerticalPanel();
        scPanel.add(new Label("SELECT FROM jp.tuyano.sample.server.entity.Employee"));
        scArea = new TextArea();
        scArea.setSize("400px", "80px");
        scPanel.add(scArea);
        Button sendButton = new Button("検索");
        sendButton.addClickHandler(new ClickHandler() {
            
            public void onClick(ClickEvent event) {
                
                searchService.searchEmployee(scArea.getText(),
                        new AsyncCallback<List<ReferenceForm>>() {
                    
                    public void onSuccess(List<ReferenceForm> result) {
                    
                        if (!result.isEmpty()) {
                            VerticalPanel panel = new VerticalPanel();
                            Label label = new Label(
                                    "検索条件に該当する従業員のデータを表示します。");
                            panel.add(label);
                            FlexTable table = new FlexTable();
                            table.setBorderWidth(2);
                            table.setText(0, 0, "ID");
                            table.setText(0, 1, "氏");
                            table.setText(0, 2, "名");
                            table.setText(0, 3, "性別");
                            table.setText(0, 4, "入社日");
                            for (int i=0; i<result.size(); i++) {
                                ReferenceForm form = result.get(i);
                                table.setText(i+1, 0, form.getId());
                                table.setText(i+1, 1, form.getLastName());
                                table.setText(i+1, 2, form.getFirstName());
                                table.setText(i+1, 3, DataConvert.
                                        convertDisplaySex(form.getSex()));
                                table.setText(i+1, 4, form.getEnterDate());
                            }
                            panel.add(table);
                            RootPanel.get("search_result_panel").clear();
                            RootPanel.get("search_result_panel").add(panel);
                        } else {
                            Label emptylabel = new Label("Empty.");
                            RootPanel.get("search_result_panel").clear();
                            RootPanel.get("search_result_panel").add(emptylabel);
                        }
                        
                        
                    }
                    
                    public void onFailure(Throwable caught) {
                    
                        Label errorlabel = new Label("SEVER ERROR!!");
                        RootPanel.get("search_result_panel").clear();
                        RootPanel.get("search_result_panel").add(errorlabel);
                    }
                });
            }
        });
        scPanel.add(sendButton);
        RootPanel.get("search_condition_panel").add(scPanel);
        
    }
}
